package c.f.d.a.a.b;

import android.content.Context;
import android.os.AsyncTask;
import c.f.a.a.a.a;
import c.f.d.a.a.b.g.b;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.Objects;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ssl.BrowserCompatHostnameVerifier;
import org.apache.http.conn.ssl.StrictHostnameVerifier;

public class c extends SSLSocketFactory {
    public static final String f = c.class.getSimpleName();
    public static volatile c g = null;
    public SSLContext a = null;
    public SSLSocket b = null;
    public Context c;
    public String[] d;
    public X509TrustManager e;

    static {
        BrowserCompatHostnameVerifier browserCompatHostnameVerifier = new BrowserCompatHostnameVerifier();
        StrictHostnameVerifier strictHostnameVerifier = new StrictHostnameVerifier();
    }

    public c(Context context) throws NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, KeyManagementException {
        if (context == null) {
            a.k(f, "SecureSSLSocketFactory: context is null");
            return;
        }
        this.c = context.getApplicationContext();
        this.a = a.d();
        String str = d.a;
        a.A(context);
        if (d.b == null) {
            synchronized (d.class) {
                if (d.b == null) {
                    InputStream h = c.f.d.a.a.b.g.a.h(context);
                    if (h == null) {
                        a.h(d.a, "get assets bks");
                        h = context.getAssets().open("hmsrootcas.bks");
                    } else {
                        a.h(d.a, "get files bks");
                    }
                    d.b = new e(h, "");
                    new b().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Context[]{context});
                }
            }
        }
        this.e = d.b;
        this.a.init(null, new X509TrustManager[]{r8}, null);
    }

    public static c b(Context context) throws IOException, NoSuchAlgorithmException, CertificateException, KeyStoreException, IllegalAccessException, KeyManagementException, IllegalArgumentException {
        a.A(context);
        if (g == null) {
            synchronized (c.class) {
                if (g == null) {
                    g = new c(context);
                }
            }
        }
        if (g.c == null && context != null) {
            c cVar = g;
            Objects.requireNonNull(cVar);
            cVar.c = context.getApplicationContext();
        }
        return g;
    }

    public final void a(Socket socket) {
        Object obj;
        Object obj2 = null;
        if (a.d(null)) {
            obj = null;
        } else {
            a.h(f, "set protocols");
            a.c((SSLSocket) socket, null);
            obj = 1;
        }
        if (!(a.d(null) && a.d(null))) {
            a.h(f, "set white cipher or black cipher");
            SSLSocket sSLSocket = (SSLSocket) socket;
            a.b(sSLSocket);
            if (a.d(null)) {
                a.a(sSLSocket, null);
            } else {
                a.e(sSLSocket, null);
            }
            obj2 = 1;
        }
        if (obj == null) {
            a.h(f, "set default protocols");
            a.b((SSLSocket) socket);
        }
        if (obj2 == null) {
            a.h(f, "set default cipher suites");
            SSLSocket sSLSocket2 = (SSLSocket) socket;
            if (sSLSocket2 != null && !a.e(sSLSocket2, a.b)) {
                a.a(sSLSocket2, a.c);
            }
        }
    }

    public Socket createSocket(String str, int i) throws IOException {
        a.h(f, "createSocket: host , port");
        Socket createSocket = this.a.getSocketFactory().createSocket(str, i);
        if (createSocket instanceof SSLSocket) {
            a(createSocket);
            SSLSocket sSLSocket = (SSLSocket) createSocket;
            this.b = sSLSocket;
            this.d = (String[]) sSLSocket.getEnabledCipherSuites().clone();
        }
        return createSocket;
    }

    public String[] getDefaultCipherSuites() {
        return new String[0];
    }

    public String[] getSupportedCipherSuites() {
        String[] strArr = this.d;
        return strArr != null ? strArr : new String[0];
    }

    public Socket createSocket(InetAddress inetAddress, int i) throws IOException {
        return createSocket(inetAddress.getHostAddress(), i);
    }

    public Socket createSocket(String str, int i, InetAddress inetAddress, int i2) throws IOException, UnknownHostException {
        return createSocket(str, i);
    }

    public Socket createSocket(InetAddress inetAddress, int i, InetAddress inetAddress2, int i2) throws IOException {
        return createSocket(inetAddress.getHostAddress(), i);
    }

    public Socket createSocket(Socket socket, String str, int i, boolean z) throws IOException {
        a.h(f, "createSocket s host port autoClose");
        socket = this.a.getSocketFactory().createSocket(socket, str, i, z);
        if (socket instanceof SSLSocket) {
            a(socket);
            SSLSocket sSLSocket = (SSLSocket) socket;
            this.b = sSLSocket;
            this.d = (String[]) sSLSocket.getEnabledCipherSuites().clone();
        }
        return socket;
    }

    public c(X509TrustManager x509TrustManager) throws NoSuchAlgorithmException, KeyManagementException, IllegalArgumentException {
        SSLContext d = a.d();
        this.a = d;
        this.e = x509TrustManager;
        d.init(null, new X509TrustManager[]{x509TrustManager}, null);
    }
}
